<?php
/**
 * 前端店铺订单数据业务处理
 * @author ome
 * @access public
 * @copyright www.shopex.net.cn 2010
 *
 */
class ome_rpc_response_order extends ome_rpc_response
{
    /**
     * 订单创建
	 * @access public
	 * @param Array $sdf 订单标准结构的数据
	 * @param Object $responseObj 框架API接口实例化对象
	 * @return array('order_id'=>'订单主键ID')
     */
    public function add($sdf, &$responseObj){
    	//var_dump($sdf);
        $log = &app::get('ome')->model('api_log');
        $logTitle = '订单创建接口[订单：'. $sdf['order_bn'] .']';
        $logInfo = '订单创建接口：<BR>';
        $logInfo .= '接收参数 $sdf 信息：' . var_export($sdf, true) . '<BR>';
        
    	$order_sdf = $sdf;
        $orderObj = &app::get('ome')->model('orders');
        $membersObj = &app::get('ome')->model('members');
        $smemberObj = &app::get('ome')->model('shop_members');
        $oGoods = &app::get('ome')->model('goods');
        $oProducts = &app::get('ome')->model('products');
        $oApi_log = &app::get('ome')->model('api_log');

        //--------------------------参数数据初始化赋值------------------------
        $order_bn = $order_sdf['order_bn'];
        //前端店铺信息
        $shop_info = $this->get_shop("shop_id,node_type,name", $responseObj);
        
        $logInfo .= '前端店铺信息：' . var_export($shop_info, true) . '<BR>';
        
        $shop_id = $shop_info['shop_id'];
        $shop_name = $shop_info['name'];
        $shop_type = $shop_info['node_type'];
        $order_sdf['shop_detail'] = $shop_info;
        $order_sdf['responseObj'] = $responseObj;
        $return_value = array('tid'=>$order_sdf['order_bn']);
        $c2c_shop_list = ome_shop_type::shop_list();
        
        $logInfo .= '订单信息：' . var_export($order_sdf, true) . '<BR>';

        $flag = 'false';#默认走通用的接口方法处理,true:表示走私有的方法
        if ($order_add_service = kernel::service('service.order.'.$shop_type)){
            if (method_exists($order_add_service, 'add')){
                $order_add_service->add($order_sdf, $flag);
                
                $logInfo .= '默认订通用接口创建成功：' . '<BR>';
            }
        }
       // echo $flag;
        if ($flag == 'true'){
            $logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
            
            return $return_value;
        }
       
        //订单详情
        $order_detail = $orderObj->dump(array('order_bn'=>$order_bn,'shop_id'=>$shop_id), 'status,pay_status,order_id,order_bn,mark_text');
        $order_id = $order_detail['order_id'];
        //买家留言
        $custom_memo = $order_sdf['custom_mark'];
        //订单备注 
        $mark_memo = $order_sdf['mark_text'];
        //买家姓名
        $member_info=json_decode($order_sdf['member_info'],true);
        $buyer_name=$member_info['name'];
        //kernel::log("add_order:".$buyer_name.var_export($expression));
        //--------------------------业务逻辑过滤处理-----------------------------
        //订单存在
        if ($order_id){
        	//echo $order_sdf['status'];
        	//订单取消
        	if ($order_sdf['status'] == 'dead' && $order_detail['status']!='dead'){
        		$memo = "订单被取消 ,同步商城";
        		$orderObj->cancel($order_id,$memo,false);
        		//echo 22222222;
        		return;
        	}
        	//未支付=》已支付        	
        	if($order_detail['pay_status'] != 1 && $order_sdf['pay_status'] == 1){
        		//更新支付为已支付，is_cod=false， 创建支付单据, 更新支付方式
        		self::pay_status_update($order_sdf, $responseObj);
        		self::payment_update($order_sdf, $responseObj);
        	}
        	
        	//非货到付款-》货到付款
        	
        	
        	//echo 11111111;
        	
           //刘向峰2012-8-30  if (in_array($shop_type, $c2c_shop_list)){
                //更新C2C类型店铺订单买家留言
            	//kernel::log("ome_order_rpc_response_order:");
            	if($mark_memo || $custom_memo){
                kernel::single('ome_order_rpc_response_order')->update_custom_mark($order_bn, $shop_id, $buyer_name, $mark_memo, $custom_memo);
            	}
                // }
            //$logInfo .= '更新订单备注：' . var_export($custom_memo, true) . '<BR>';
            //$logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
            //$log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
            
            return false;
        }
  //echo 'ttttttt';
        //订单状态业务规则过滤
    	$shipping = json_decode($order_sdf['shipping'],true);
        if (!empty($shipping)){
    	    $is_cod =  $shipping['is_cod']?$shipping['is_cod']:'false';
    	}else{
    	    $is_cod = 'false';	
    	}
    	
    	$logInfo .= '订单状态业务规则 $is_cod 为：' . var_export($is_cod, true) . '<BR>';
    	
        $order_filter = array(
            'order_bn' => $order_bn,
            'shop_id' => $shop_id,
            'shop_type' => $shop_type,
            'shop_name' => $shop_name,
            'status' => $order_sdf['status'],
            'pay_status' => $order_sdf['pay_status'],
            'ship_status' => $order_sdf['ship_status'],
	        'is_cod' => $is_cod
        );
        $order_filter_result = ome_order_rpc_response_order::order_filter($order_filter);
       var_dump($order_filter_result);
        if ($order_filter_result['status'] == false){
            if ($order_filter_result['rsp'] == 'succ'){
                
                $logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
               // $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'fail', $logInfo);
            
                return false;
            }else{
                $msg = $order_filter_result['res'];
                $logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
                // $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'fail', $logInfo);
                    
                if ($order_sdf['order_from'] == 'omeapiplugin'){
                    return false;
                }else{
                   // $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'fail', $logInfo);
                    return false;
                    //$responseObj->send_user_error(app::get('base')->_($msg), $return_value);
                }
            }
        }
        
        //本地标准SDF格式数据转换
        $order_sdf = kernel::single('ome_order_rpc_response_order')->order_sdf_convert($order_sdf);
        
        $logInfo .= '本地标准SDF格式信息：' . var_export($order_sdf, true) . '<BR>';
        
        //订单货品明细业务规则过滤
        $order_objects = json_decode($order_sdf['order_objects'],true);
        $product_status = ome_order_rpc_response_order::order_objects_filter($order_bn, $shop_id, $order_objects, $responseObj);
        $order_sdf['order_objects'] = $order_objects;
        
        //将订单的已支付金额与支付状态初始化，即支付金额为0，支付状态为0（未支付）
        //TODO：此步操作是为了先将所有的订单为未支付，然后会在生成支付单的时候根据支付单数据改变订单的已支付金额与支付状态
        $tmp_payed = $order_sdf['payed'];
        $tmp_pay_status = $order_sdf['pay_status'];
        /*2012-8-1 刘向峰 
        $order_sdf['payed'] = '0';
        $order_sdf['pay_status'] = '0';
        */
        
        //更新前端店铺会员信息
        if ($order_sdf['member_info']){
            $member_id = kernel::single('ome_order_rpc_response_member')->update_order_member($order_sdf['member_info'],$shop_id);
            if ($member_id){
                $order_sdf['member_id'] = $member_id;
            }
            
            $logInfo .= '更新前端店铺会员信息，会员ID：' . var_export($member_id, true) . '<BR>';
        }else{
            unset($order_sdf['member_id']);
        }
        
        //TODO:订单批次索引号
        if (!empty($order_sdf['member_id'])){
            if ($service = kernel::servicelist('service.order')){
                foreach ($service as $object => $instance){
                    if (method_exists($instance, 'order_job_no')){
                        $order_sdf['order_job_no'] = $instance->order_job_no($order_sdf, 'get');
                        
                        $logInfo .= '订单批次索引号：' . var_export($$order_sdf['order_job_no'], true) . '<BR>';
                    }
                }
            }
        }
        
        //订单创建
        if ($product_status==false){
            $order_sdf['is_fail'] = 'true';
            $order_sdf['edit_status'] = 'true';
        }
        if ($orderObj->create_order($order_sdf)){
        
            //更新店铺下载订单时间
            $shopObj = &app::get('ome')->model('shop');
            $shopdata = array('last_download_time'=>time());
            $shopObj->update($shopdata, array('shop_id'=>$shop_id));
            
            //前端店铺生成支付单 
            //if (!in_array($shop_type, $c2c_shop_list)){
                if ($tmp_pay_status == '1'){
                    $payment_detail = json_decode($order_sdf['payment_detail'], true);
                    $payment_detail['pay_time'] = kernel::single('ome_func')->date2time($payment_detail['pay_time']);
                    $payment_sdf = array(
                    	'payment_bn'=> $payment_detail['payment_bn'],
                        'status' => 'succ',
                        'money' => $tmp_payed,
                        'order_bn' => $order_bn,
                        'pay_account' => $payment_detail['pay_account'],
                        'paymethod' => $payment_detail['paymethod'],
                        'trade_no' => $payment_detail['trade_no'],
                        't_begin' => $payment_detail['pay_time'],
                        'source_shop' => $shop_type,
                    );
                    kernel::single("ome_rpc_response_payment")->add($payment_sdf, $responseObj);
                    
                    $logInfo .= '前端店铺增加支付单，信息：' . var_export($payment_sdf, true) . '<BR>';
               }
            //}
            
            //优惠方案
            $pmt_detail = json_decode($order_sdf['pmt_detail'], true);
            
            $logInfo .= '优惠方案信息：' . var_export($pmt_detail, true) . '<BR>';
            
            if (!empty($pmt_detail)){
                kernel::single('ome_order_rpc_response_order')->add_order_pmt($order_sdf['order_id'], $pmt_detail);            
            }
            
            //更新代销人信息
            $selling_agent = json_decode($order_sdf['selling_agent'], true);
            
            $logInfo .= '代销人信息：' . var_export($selling_agent, true) . '<BR>';
            
            if (!empty($selling_agent['member_info']['uname'])){
                kernel::single('ome_order_rpc_response_order')->update_selling_agent_info($order_sdf['order_id'], $selling_agent);
            }
            
            $logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
            
            return $return_value;
        }else{
            $msg = '订单插入失败,请检查相关字段类型是否不匹配或者存在相同订单号!';
            $logInfo .= '异常消息：' . $msg . '<BR>';
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'fail', $logInfo);
            
            $responseObj->send_user_error(app::get('base')->_($msg), $return_value);
        }
        
        $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    }

   /**
     * 更新订单状态
     * @access public
     * @param Array $order_sdf 待更新订单状态标准结构数据
     * @param Object $responseObj 框架API接口实例化对象
     */
    public function status_update($order_sdf, &$responseObj){
        $log = &app::get('ome')->model('api_log');
        $logTitle = '更新订单状态接口['. $order_sdf['order_bn'] .']';
        $logInfo = '更新订单状态接口：<BR>';
        $logInfo .= '接收参数 $order_sdf 信息：' . var_export($order_sdf, true) . '<BR>';

    	$shop_info = $this->get_shop("shop_id,shop_type,name", $responseObj);
    	
    	$logInfo .= '店铺信息：' . var_export($shop_info, true) . '<BR>';
            
        $shop_id = $shop_info['shop_id'];
        $shop_type = $shop_info['shop_type'];
    	$orderObj = &app::get('ome')->model('orders');
	
    	$status = $order_sdf['status'];
    	$order_bn = $order_sdf['order_bn'];
    	
    	//返回值
        $return_value = array('tid'=>$order_bn);
        
    	if ($status==''){
    	    $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    	    
    		$responseObj->send_user_error(app::get('base')->_('Order status '.$status.' is not exists'), $return_value);
    	}
    	$order_info = kernel::database()->selectrow("SELECT pay_status,order_id,op_id FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
    	
    	$logInfo .= '订单信息：' . var_export($order_info, true) . '<BR>';
    	
    	if(!empty($order_info)){
    		
    		$order_id = $order_info['order_id'];
    		
	    	if (!$order_info['op_id']){
	    		$user_info = kernel::database()->selectrow("SELECT user_id FROM sdb_desktop_users WHERE super='1' ORDER BY user_id asc ");
        		$op_id = $user_info['user_id'];
        		$op_idsql = ",op_id='".$op_id."'";
	    	}
	    	
	    	kernel::database()->exec("UPDATE sdb_ome_orders SET status='$status'$op_idsql WHERE order_id='".$order_id."'");
            
	    	$logInfo .= '更新订单状态：' . var_export($status, true) . '<BR>';
    		if ($status == 'dead'){//订单取消
    		    //B2B业务逻辑处理:已支付/部分支付/部分退款的直接返回已支付状态,不作取消订单操作
    		    $b2b_shop_list = ome_shop_type::b2b_shop_list();
    		    
    		    $logInfo .= 'b2b shop list：' . var_export($b2b_shop_list, true) . '<BR>';
            
    		    if (in_array($shop_type,$b2b_shop_list) && in_array($order_info['pay_status'],array('1','2','3','4'))){
                    $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    		        
    		        $responseObj->send_user_error(app::get('base')->_('Order '.$order_bn.' has been paid'), $return_value);
    		    }else{
    		        
    		        $logInfo .= '取消订单，ID为：' . $order_id . '<BR>';
            
	    		    $orderObj->cancel($order_id,"订单被取消");
	    	    }
	    	}
	    	
	    	$logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
            
            return $return_value;
    	}else{
    	    if ($status == 'dead'){
        	    //取消失败订单
                if (app::get('omeapilog')->is_installed()){
                    $fail_orderObj = &app::get('omeapilog')->model('orders');
                    $result = $fail_orderObj->cancel_order($order_bn, $shop_id);
                    
                    $logInfo .= '取消店铺ID为：' . $shop_id . ' 的失败订单ID：' . $order_bn . '<BR>';
                }
    	    }
    	    if (!$result){
    		    $logInfo .= '取消失败订单没有成功<BR>';
                $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    		    
    		    $responseObj->send_user_error(app::get('base')->_('Order Order_bn '.$order_bn.' is not exists'), $return_value);
    		    
    	    }
        }
        
        $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    }
    
    /**
     * 更新订单支付状态
     * @access public
     * @param Array $order_sdf  待更新订单支付状态标准结构数据
     * @param Object $responseObj  框架API接口实例化对象
     */
    public function pay_status_update($order_sdf, &$responseObj){
        $log = &app::get('ome')->model('api_log');
        $logTitle = '更新订单支付状态接口['. $order_sdf['order_bn'] .']';
        $logInfo = '更新订单状支付态接口：<BR>';
        $logInfo .= '接收参数 $order_sdf 信息：' . var_export($order_sdf, true) . '<BR>';
        
    	$shop_id = $this->get_shop_id($responseObj);
    	
    	$status = $order_sdf['pay_status'];
    	$order_bn = $order_sdf['order_bn'];
    	
    	//返回值
        $return_value = array('tid'=>$order_bn);
    	
    	if ($status==''){
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    	    
    		$responseObj->send_user_error(app::get('base')->_('Order status '.$status.' is not exists'), $return_value);
    	}
    	$order_info = kernel::database()->selectrow("SELECT order_id FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
    	
    	if(!empty($order_info)){
    		
    		$order_id = $order_info['order_id'];
    		kernel::database()->exec("UPDATE sdb_ome_orders SET pay_status='$status' WHERE order_id='$order_id'");
            
    		$logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    		
            return $return_value;
    		
    	}else{
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
            
    		$responseObj->send_user_error(app::get('base')->_('Order_bn: '.$order_bn.' is not exists'), $return_value);
    	}
    	
        $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    }
    
    /**
     * 更新订单发货状态
     * @access public
     * @param Array $order_sdf 待更新订单发货状态标准结构数据
     * @param Object $responseObj  框架API接口实例化对象
     */
    public function ship_status_update($order_sdf, &$responseObj){
        $log = &app::get('ome')->model('api_log');
        $logTitle = '更新订单发货状态接口['. $order_sdf['order_bn'] .']';
        $logInfo = '更新订单状发货态接口：<BR>';
        $logInfo .= '接收参数 $order_sdf 信息：' . var_export($order_sdf, true) . '<BR>';
        
    	$shop_id = $this->get_shop_id($responseObj);
    	
    	$status = $order_sdf['ship_status'];
    	$order_bn = $order_sdf['order_bn'];
    	
    	//返回值
        $return_value = array('tid'=>$order_bn);
    	
    	if ($status==''){
    	    $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    	    
    		$responseObj->send_user_error(app::get('base')->_('Order status '.$status.' is not exists'), $return_value);
    	}
    	$order_info = kernel::database()->selectrow("SELECT order_id FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
    	
    	$logInfo .= '订单信息：' . var_export($order_info, true) . '<BR>';
    	
    	if(!empty($order_info)){
    		
    		$order_id = $order_info['order_id'];
    		kernel::database()->exec("UPDATE sdb_ome_orders SET ship_status='$status' WHERE order_id='$order_id'");
    		
    		$logInfo .= '返回值为：' . var_export($return_value, true) . '<BR>';
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
            
            return $return_value;
            
    	}else{
            $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
        
    		$responseObj->send_user_error(app::get('base')->_('Order_bn: '.$order_bn.' is not exists'), $return_value);
    	}
    	
        $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
    }
    
    /**
     * 添加买家留言
     * @access public
     * @param Array $order_sdf 买家留言标准结构数据
     * @param Object $responseObj 框架API接口实例化对象
     */
    public function custom_mark_add($order_sdf, &$responseObj){

        $shop_id = $this->get_shop_id($responseObj);
        
        $order_bn = $order_sdf['tid'];
        $op_content = $order_sdf['message'];
        $op_name = $order_sdf['sender'];
        $op_time = kernel::single('ome_func')->date2time($order_sdf['add_time']);
        $order_info = kernel::database()->selectrow("SELECT order_id,custom_mark FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
        
        //返回值
        $return_value = array('tid'=>$order_bn);
        
        if(!empty($order_info)){
            $order_id = $order_info['order_id'];
            $orderObj = &app::get('ome')->model('orders');
            
            //取出买家留言信息
            $oldmemo= unserialize($order_info['custom_mark']);
            if ($oldmemo)
            foreach($oldmemo as $k=>$v){
                $custom_memo[] = $v;
            }
            $newmemo =  htmlspecialchars($op_content);
            $newmemo = array('op_name'=>$op_name, 'op_time'=>$op_time, 'op_content'=>$newmemo);
            $custom_memo[] = $newmemo;
            $order_memo['custom_mark'] = serialize($custom_memo);
            $filter = array('order_id'=>$order_id);
            $orderObj->update($order_memo, $filter);
            
            return $return_value;
            
        }else{
        	$responseObj->send_user_error(app::get('base')->_('Order: '.$order_bn.' is not exists'), $return_value);
        }
    }
    
    /**
     * 更新买家留言
     * @access public
     * @param Array $order_sdf 买家留言标准结构数据
     * @param Object $responseObj 框架API接口实例化对象
     */
    public function custom_mark_update($order_sdf, &$responseObj){

        $shop_id = $this->get_shop_id($responseObj);
        
        $order_bn = $order_sdf['tid'];
        $op_content = $order_sdf['message'];
        $op_name = $order_sdf['sender'];
        $op_time = kernel::single('ome_func')->date2time($order_sdf['add_time']);
        $order_info = kernel::database()->selectrow("SELECT order_id,custom_mark FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
        
        //返回值
        $return_value = array('tid'=>$order_bn);
        
        if(!empty($order_info)){
            $order_id = $order_info['order_id'];
            $orderObj = &app::get('ome')->model('orders');
            
            //取出买家留言信息
            $oldmemo= unserialize($order_info['custom_mark']);
            if ($oldmemo)
            foreach($oldmemo as $k=>$v){
                $custom_memo[] = $v;
            }
            $newmemo =  htmlspecialchars($op_content);
            $newmemo = array('op_name'=>$op_name, 'op_time'=>$op_time, 'op_content'=>$newmemo);
            $custom_memo[] = $newmemo;
            $order_memo['custom_mark'] = serialize($custom_memo);
            $filter = array('order_id'=>$order_id);
            $orderObj->update($order_memo, $filter);
            
            return $return_value;
            
        }else{
            $responseObj->send_user_error(app::get('base')->_('Order: '.$order_bn.' is not exists'), $return_value);
        }
    }
    
    /**
     * 添加订单备注
     * @access public
     * @param Array $order_sdf 订单备注标准结构数据
     * @param Object $responseObj 框架API接口实例化对象
     */
    public function memo_add($order_sdf, &$responseObj){

        $shop_id = $this->get_shop_id($responseObj);
        
        $order_bn = $order_sdf['tid'];
        $op_content = $order_sdf['memo'];
        $op_name = $order_sdf['sender'];
        $op_time = kernel::single('ome_func')->date2time($order_sdf['add_time']);
        $order_info = kernel::database()->selectrow("SELECT order_id,mark_text FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
        
        //返回值
        $return_value = array('tid'=>$order_bn);
        
        if(!empty($order_info)){
            $order_id = $order_info['order_id'];
            $orderObj = &app::get('ome')->model('orders');
            
            //取出订单备注信息
            $oldmemo= unserialize($order_info['mark_text']);
            if ($oldmemo)
            foreach($oldmemo as $k=>$v){
                $custom_memo[] = $v;
            }
            $newmemo =  htmlspecialchars($op_content);
            $newmemo = array('op_name'=>$op_name, 'op_time'=>$op_time, 'op_content'=>$newmemo);
            $custom_memo[] = $newmemo;
            $order_memo['mark_text'] = serialize($custom_memo);
            $order_memo['mark_type'] = $order_sdf['flag'];
            $filter = array('order_id'=>$order_id);
            $orderObj->update($order_memo, $filter);
            
            return $return_value;
            
        }else{
        	$responseObj->send_user_error(app::get('base')->_('Order: '.$order_bn.' is not exists'), $return_value);
        }
    }
    
    
    /**
     * 更新订单备注
     * @access public
     * @param Array $order_sdf 订单备注注标准结构数据
     * @param Object $responseObj 框架API接口实例化对象
     */
    public function memo_update($order_sdf, &$responseObj){

        $shop_id = $this->get_shop_id($responseObj);
        
        $order_bn = $order_sdf['tid'];
        $op_content = $order_sdf['memo'];
        $op_name = $order_sdf['sender'];
        $op_time = kernel::single('ome_func')->date2time($order_sdf['add_time']);
        $order_info = kernel::database()->selectrow("SELECT order_id,mark_text FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
        
        //返回值
        $return_value = array('tid'=>$order_bn);
        
        if(!empty($order_info)){
            $order_id = $order_info['order_id'];
            $orderObj = &app::get('ome')->model('orders');
            
            //取出订单备注信息
            $oldmemo= unserialize($order_info['mark_text']);
            if ($oldmemo)
            foreach($oldmemo as $k=>$v){
                $custom_memo[] = $v;
            }
            $newmemo =  htmlspecialchars($op_content);
            $newmemo = array('op_name'=>$op_name, 'op_time'=>$op_time, 'op_content'=>$newmemo);
            $custom_memo[] = $newmemo;
            $order_memo['mark_text'] = serialize($custom_memo);
            $order_memo['mark_type'] = $order_sdf['flag'];
            $filter = array('order_id'=>$order_id);
            $orderObj->update($order_memo, $filter);
            
            return $return_value;
            
        }else{
        	$responseObj->send_user_error(app::get('base')->_('Order: '.$order_bn.' is not exists'), $return_value);
        }
    }

    public function payment_update($order_sdf, &$responseObj){
        $log = &app::get('ome')->model('api_log');
        $logTitle = '更新支付单状态接口 payment_update [订单:'. $order_sdf['order_bn'] .']';
        $logInfo = '更新支付单状态接口 payment_update：<BR>';
        $logInfo .= '接收参数 $order_sdf 信息：' . var_export($order_sdf, true) . '<BR>';
        
        $shop_id = $this->get_shop_id($responseObj);
        $order_bn = $order_sdf['order_bn'];
        
        $logInfo .= '店铺ID：' . var_export($shop_id, true) . '<BR>';

        $order_info = kernel::database()->selectrow("SELECT order_id,mark_text,cost_payment,total_amount,final_amount FROM sdb_ome_orders WHERE order_bn='".$order_bn."' AND shop_id='".$shop_id."'");
        
        //返回值
        $return_value = array('tid'=>$order_bn);
        
        $log->write_log($log->gen_id(), $logTitle, __CLASS__, __METHOD__, '', '', 'response', 'success', $logInfo);
        
        if(!empty($order_info)){
            $orderObj = &app::get('ome')->model('orders');
            $order_id = $order_info['order_id'];

            $order_payment['pay_bn'] = $order_sdf['pay_bn'];
            $order_payment['payment'] = $order_sdf['payment'];
            //支付费用修改
            $order_payment['cost_payment'] = $order_sdf['cost_payment'];
            //从新计算订单总额
            $order_payment['total_amount'] = $order_info['total_amount'] - $order_info['cost_payment'] + $order_sdf['cost_payment'];
            $order_payment['final_amount'] = $order_payment['total_amount'];
            $filter = array('order_id'=>$order_id);

            $orderObj->update($order_payment, $filter);
            
            return $return_value;
        }else{
        	$responseObj->send_user_error(app::get('base')->_('Order: '.$order_bn.' is not exists'), $return_value);
        }
    }
}
?>